package com.davy.gridpoint.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.davy.gridpoint.R;

import java.util.ArrayList;
import java.util.Map;


public class PointListAdapter extends RecyclerView.Adapter<PointListAdapter.ItemViewHolder> {
    private ItemClickListener mItemClick;
    private Context mContext;
    private ArrayList<Map> mList;

    public PointListAdapter(Context mContext, ArrayList<Map> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public void setData(ArrayList<Map> vList) {
        this.mList = vList;
        notifyDataSetChanged();
    }

    public void setItemClickListener(ItemClickListener listener) {
        this.mItemClick = listener;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder itemViewHolder = new ItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.itme_pointlist, parent, false));
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        if (mList.size() > 0) {
            holder.GeoGridName.setText(mList.get(position).get("GeoGridName") + "");
            holder.LevelName.setText(mList.get(position).get("LevelName") + "");
            holder.RailLeaderName.setText(mList.get(position).get("RailLeaderName") + "");
            holder.RailLeaderPhone.setText(mList.get(position).get("RailLeaderPhone") + "");

            if (mList.size() == position + 1) {
                holder.v_line.setVisibility(View.INVISIBLE);
            } else {
                holder.v_line.setVisibility(View.VISIBLE);
            }

            holder.itemView.setOnClickListener(v -> {
                if (mItemClick != null) {
                    mItemClick.onClick(position, mList.get(position));
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView GeoGridName, LevelName, RailLeaderName, RailLeaderPhone;
        private View v_line;

        public ItemViewHolder(View itemView) {
            super(itemView);
            RailLeaderPhone = itemView.findViewById(R.id.RailLeaderPhone);
            v_line = itemView.findViewById(R.id.v_line);
            GeoGridName = itemView.findViewById(R.id.GeoGridName);
            LevelName = itemView.findViewById(R.id.LevelName);
            RailLeaderName = itemView.findViewById(R.id.RailLeaderName);
        }
    }

    public interface ItemClickListener {
        void onClick(int pos, Map mData);
    }
}
